package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.dataleveltyperel.DataLevelTypeRelCondition;
import com.bwda.dsrs.filecenter.domain.po.datalevel.DataLevelPo;
import com.bwda.dsrs.filecenter.domain.condition.datalevel.DataLevelCondition;
import com.bwda.dsrs.filecenter.domain.form.datalevel.DataLevelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.datalevel.DataLevelUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.datalevel.DataLevelCreateForm;
import com.bwda.dsrs.filecenter.domain.po.dataleveltyperel.DataLevelTypeRelPo;
import com.bwda.dsrs.filecenter.domain.po.datatype.DataTypePo;
import com.bwda.dsrs.filecenter.domain.vo.datalevel.DataLevelVo;
import com.bwda.dsrs.filecenter.service.DataLevelService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.service.DataLevelTypeRelService;
import com.bwda.dsrs.filecenter.service.DataTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/datalevel")
@Api(value = "/b/datalevel", tags = "数据分级表")
public class DataLevelController extends BaseController {

	@Autowired
	private DataLevelService dataLevelService;

	@Autowired
	private DataLevelTypeRelService dataLevelTypeRelService;

	@Autowired
	private DataTypeService dataTypeService;

	@ApiOperation(value = "查询数据分级表",notes = "查询数据分级表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataLevelVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer levelId) throws BusinessServiceException {
		DataLevelPo po = dataLevelService.queryWithValid(levelId);
		DataLevelVo vo = CopyUtil.transfer(po, DataLevelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据分级表数量",notes = "查询数据分级表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DataLevelQueryForm form) throws BusinessServiceException {
		DataLevelCondition condition = this.getConditionByQueryForm(form);
		int count = dataLevelService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据分级表列表",notes = "查询数据分级表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataLevelVo>> queryList(@ModelAttribute@Valid DataLevelQueryForm form) throws BusinessServiceException {
		DataLevelCondition condition = this.getConditionByQueryForm(form);
		List<DataLevelPo> poList = dataLevelService.queryList(condition);
		List<DataLevelVo> voList = CopyUtil.transfer(poList, DataLevelVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据分级表列表(带分页)",notes = "查询数据分级表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DataLevelVo>> queryPageList(@ModelAttribute@Valid DataLevelQueryForm form) throws BusinessServiceException {
		logger.info("查询数据分级表列表(带分页)");
		DataLevelCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DataLevelVo> voList = new ArrayList<>();
		int count = dataLevelService.queryCount(condition);
		if (count > 0) {
			List<DataLevelPo> poList = dataLevelService.queryList(condition);
			voList = CopyUtil.transfer(poList, DataLevelVo.class);
			for (DataLevelVo dataLevelVo:voList){
				//组装list 返回给前端
				DataLevelTypeRelCondition dataLevelTypeRelCondition = new DataLevelTypeRelCondition();
				dataLevelTypeRelCondition.setLevelId(dataLevelVo.getLevelId());
				List<DataLevelTypeRelPo> list =dataLevelTypeRelService.queryList(dataLevelTypeRelCondition);
				String boundNames ="";
				String boundIds ="";
				if (list.size()>0){
					for (DataLevelTypeRelPo dataLevelTypeRelPo:list){
						boundIds=boundIds+","+dataLevelTypeRelPo.getTypeId();
						DataTypePo dataTypePo =dataTypeService.query(dataLevelTypeRelPo.getTypeId());
						boundNames=boundNames+","+dataTypePo.getTypeName();
					}
					boundIds =boundIds.substring(1);
					boundNames =boundNames.substring(1);
					dataLevelVo.setBoundIds(boundIds);
					dataLevelVo.setBoundNames(boundNames);
				}
			}
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增数据分级表",notes = "新增数据分级表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataLevelVo> add(@ModelAttribute@Valid DataLevelCreateForm form) throws BusinessServiceException {
		logger.info("新增数据分级");
		DataLevelPo po = CopyUtil.transfer(form, DataLevelPo.class);
		Integer levelId  = dataLevelService.queryId();
		//1对多的关系
		if (StringUtils.isNotBlank(form.getTypeIds())){
			String[] typeIds =form.getTypeIds().split(",");
			for (String str:typeIds){
				DataLevelTypeRelPo dataLevelTypeRelPo = new DataLevelTypeRelPo();
				dataLevelTypeRelPo.setLevelId(levelId);
				dataLevelTypeRelPo.setTypeId(Integer.parseInt(str));
				dataLevelTypeRelService.insert(dataLevelTypeRelPo);
			}
		}
		po.setLevelId(levelId);
		dataLevelService.insert(po);
		DataLevelVo vo = CopyUtil.transfer(po, DataLevelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据分级表",notes = "修改数据分级表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DataLevelUpdateForm form) throws BusinessServiceException {
		logger.info("修改数据分级");
		DataLevelPo po = CopyUtil.transfer(form, DataLevelPo.class);
		dataLevelTypeRelService.deleteByLevelId(form.getLevelId());
		//1对多的关系
		if (StringUtils.isNotBlank(form.getTypeIds())) {
			String[] typeIds = form.getTypeIds().split(",");
			for (String str : typeIds) {
				DataLevelTypeRelPo dataLevelTypeRelPo = new DataLevelTypeRelPo();
				dataLevelTypeRelPo.setLevelId(form.getLevelId());
				dataLevelTypeRelPo.setTypeId(Integer.parseInt(str));
				dataLevelTypeRelService.insert(dataLevelTypeRelPo);
			}
		}
		dataLevelService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据分级表",notes = "删除数据分级表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "levelIds", required = true)@RequestParam String levelIds) throws BusinessServiceException {
		logger.info("删除数据分级");
		String[] levelId = levelIds.split(",");
		//支持批量
		for (String str:levelId){
			dataLevelService.delete(Integer.parseInt(str));
			//删除关联
			dataLevelTypeRelService.deleteByLevelId(Integer.parseInt(str));
		}
		return getSuccessResult();
	}

	@ApiOperation(value = "查询数据分级表名称列表",notes = "查询数据分级表名称列表",httpMethod = "GET")
	@RequestMapping(value = "/queryNameList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<String>> queryNameList(@ModelAttribute@Valid DataLevelQueryForm form) throws BusinessServiceException {
		DataLevelCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DataLevelPo> poList = dataLevelService.queryList(condition);
		List<String> voList = new ArrayList<>();
		if(!CollectionUtils.isEmpty(poList)){
			voList = poList.stream().map(p->p.getName()).collect(Collectors.toList());
		}
		return getSuccessResult(getListResponse(voList));
	}
	/**
	 * DataLevelQueryForm转换为DataLevelCondition
	 *
	 * @param form
	 * @return
	 */
	private DataLevelCondition getConditionByQueryForm(DataLevelQueryForm form) {
		DataLevelCondition condition = CopyUtil.transfer(form, DataLevelCondition.class);
		return condition;
	}

}